package org.example.dev.learn.collection;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ArrayBlockingQueue;

@Slf4j
public class Learn2ArrayBlockingQueue {
    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
        int len = 10;
        Thread t0 = new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                for(int i = 0; i < len; i++){
                    queue.put("money");
                    log.info("线程：{}，开始往队列里放钱了，这是第{}次放钱", Thread.currentThread().getName(), i);
                }

            }
        },"t0");
        Thread t1 = new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                for(int i = 0; i < len; i++){
                    queue.take();
                    log.info("线程：{}，取钱，取钱……这是第{}次取钱", Thread.currentThread().getName(), i);
                }

            }
        },"t1");
        t0.start();
        Thread.sleep(3000);
        t1.start();
    }
}
